﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace ElectronicCommerce.Framework.ServiceModel.WCF
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TChannel"></typeparam>
    public class ClientServiceProxy<TChannel> : IDisposable where TChannel : class
    {
        private ChannelFactory<TChannel> channelFactory = null;
        private TChannel channelInternal = null;

        /// <summary>
        /// 
        /// </summary>
        public ClientServiceProxy()
        {
            channelFactory = new ClientChannelFactory<TChannel>();
            channelInternal = channelFactory.CreateChannel();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceName"></param>
        public ClientServiceProxy(string serviceName)
        {
            channelFactory = new ClientChannelFactory<TChannel>(serviceName);
            channelInternal = channelFactory.CreateChannel();
        }

        /// <summary>
        /// 
        /// </summary>
        public TChannel Channel
        {
            get
            {
                return channelInternal;
            }
        }

        #region IDisposable Members

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            if (channelFactory != null &&
                channelFactory.State == CommunicationState.Opened)
            {
                channelFactory.Close();
            }
        }

        #endregion
    }
}
